import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class SQLiteUtils {
	
	private String dbPath;
	
	public SQLiteUtils(String path) {
		dbPath = path;
	}
	
	public ArrayList<String> getListTables() {
		ArrayList<String> tables = new ArrayList<String>();
		Connection conn = null;
		Statement stm = null;
		ResultSet rs = null;
		try {
			Class.forName("org.sqlite.JDBC");
			conn = DriverManager.getConnection("jdbc:sqlite:"+dbPath);
			stm = conn.createStatement();
			rs = stm.executeQuery("SELECT tbl_name FROM sqlite_master WHERE type='table' AND name != 'sqlite_sequence'");
			while(rs.next()) {
				tables.add(rs.getString(1));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try 
			{
				if (rs != null) rs.close();
				if (stm != null) stm.close();
				if (conn != null) conn.close();
			} 
			catch(Exception ex) {}
		}
		return tables;
	}
	
	public Map<String, String> getMetaData(String tableName) {
		Map<String, String> meta = new HashMap<String, String>();
		Connection conn = null;
		Statement stm = null;
		ResultSet rs = null;
		try {
			Class.forName("org.sqlite.JDBC");
			conn = DriverManager.getConnection("jdbc:sqlite:"+dbPath);
			stm = conn.createStatement();
			rs = stm.executeQuery("SELECT * FROM "+tableName);
			ResultSetMetaData rsm = rs.getMetaData();
			
			for (int i=1;i<=rsm.getColumnCount();i++) {
				meta.put(rsm.getColumnName(i), rsm.getColumnTypeName(i));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				stm.close();
				conn.close();
			} catch(Exception ex) {
			}
		}
		return meta;
	}
}
